package controller;

import dao.DaoException;
import dao.productDao.ProductDaoImpl;
import model.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "ShowServlet", urlPatterns = "/showProducts")
public class ShowServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        ProductDaoImpl dao = new ProductDaoImpl();
        ArrayList<Product> productList = null;
        try {
            productList = dao.findProductsByType(type);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        request.setAttribute("products", productList);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}
